package com.jservice.web.authc;

import com.jfinal.aop.Clear;
import com.jservice.framework.annotation.RouteBind;
import com.jservice.framework.authc.AuthcInterceptor;
import com.jservice.framework.dto.Message;
import com.jservice.model.SysUser;
import com.jservice.web.WebConstant;
import com.jservice.web.base.JBaseController;
import com.jservice.web.sysUser.SysUserService;

/**
 * LoginController
 *
 * @Author cwledit
 * @DATE 2017/5/28.
 */
@RouteBind(path = "/admin/authc")
@Clear(AuthcInterceptor.class)
public class AuthcController extends JBaseController{
    @Clear(AuthcInterceptor.class)
    public void login(){
        render("/admin/authc/login.html");
    }
    @Clear(AuthcInterceptor.class)
    public void doLogin(){
        if (!validateCaptcha("validateCode")){
            setMessage(Message.error("验证码错误!"));
            render("/admin/authc/login.html");
            return;
        }
        String loginName = getPara("username");
        String password = getPara("password");
        String result = AuthcService.me().valid(loginName,password);
        if ("success".equals(result)){
            SysUser sysUser = SysUserService.me().findByLoginName(loginName);
            setSessionAttr(WebConstant.request_cUser,sysUser);
            redirect("/admin/index");
        }else{
            setMessage(Message.error(result));
            render("/admin/authc/login.html");
        }
    }
    @Clear(AuthcInterceptor.class)
    public void loginOut(){
        removeSessionAttr(WebConstant.request_cUser);
        redirectLogin();
    }
    @Clear(AuthcInterceptor.class)
    public void validatecode(){
        renderCaptcha();
    }







}
